//
//  SMHMineView.swift
//  SMHCommerce
//
//  Created by lvzhao on 2020/6/3.
//  Copyright © 2020 lvzhao. All rights reserved.
//

import UIKit
import SwiftyFitsize

class SMHMineView: LZBaseView,UICollectionViewDelegate,UICollectionViewDataSource,UIScrollViewDelegate{

    var viewModel = SMHMineViewModel()
    var mineNavView : SMHMineNavView?
    var topImageView : UIImageView?

    override init(viewModel: LZBaseViewModel) {
        super.init(viewModel: viewModel)
        self.viewModel = viewModel as! SMHMineViewModel
        
        self.setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() {
        
        
        let imageView = UIImageView.init(image: UIImage.init(named: "icon_mine_background"))
        self.topImageView = imageView
        addSubview(imageView)
        imageView.snp.makeConstraints { (make) in
            make.left.top.equalTo(0)
            make.width.equalTo(kScreenWidth)
            make.height.equalTo(kisPhoneX() ? 347 : 323)
        }
        
        self.addSubview(self.collectionView)
        self.collectionView.snp.makeConstraints { (make) in
            make.edges.equalTo(0)
        }
    
        self.collectionView .register(SMHMineProductCell.self, forCellWithReuseIdentifier: "SMHMineProductCell")
        self.collectionView .register(SMHMineHeadView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "SMHMineHeadView")
        self.collectionView .register(SMHMineFootView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: "SMHMineFootView")


        //导航
        self.mineNavView = SMHMineNavView.init(viewModel: self.viewModel)
        self.mineNavView!.alpha = 0
        addSubview(self.mineNavView!)
        self.mineNavView?.snp.makeConstraints({ (make) in
            make.left.top.right.equalTo(0)
            make.height.equalTo(kNavHeight)
        })
        
        


    }
    
    
    //MARK:UICollectionViewDataSource
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 40
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let productCell = collectionView .dequeueReusableCell(withReuseIdentifier: "SMHMineProductCell", for: indexPath) as! SMHMineProductCell
        productCell.backgroundColor = UIColorRandom()
        return productCell

    }

    //MARK:UICollectionViewDelegate
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        var reusableview:UICollectionReusableView!
        if kind == UICollectionView.elementKindSectionHeader{
            reusableview = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "SMHMineHeadView", for: indexPath) as! SMHMineHeadView
        } else {
            reusableview = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "SMHMineFootView", for: indexPath) as! SMHMineFootView
        }
        return reusableview
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
           
        
        LZAlertViewController.systemAlter(.sysAlert, alterTitle: "123", message: "345", cancel: "取消", sure: "确认", dictionary: nil, cancelBlock: { (cancel) in
            print(cancel!)
            
        }) { (sure) in
            
            print(sure!)

        }
//        
//        LZAlertViewController.systemAlter(.sysAlert, alterTitle: "选择照片", message: nil, cancel: "取消", sure: "确定", objectDict: nil, cancelBlock: { (cancel) in
//            
//        }) { (sure) in
//            
//            
//        }
        
//        let dataArray : [String] = ["相册","拍一张","取消"];
//
//        let sheetVC = LZAlertViewController.init(alert: .sysAlert, title: "选择一张照片", message: "", dictionary: ["dataArray":dataArray as AnyObject])
//           sheetVC!.sureBlock = { index in
//               print(index)
//           }
//        currentVC()!.present(sheetVC!, animated: false, completion:  nil)
//
       }
    
    //MARK:UIScrollViewDelegate

    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        
        //图片上下偏移量
        let imageOffsetY = scrollView.contentOffset.y;
        self.mineNavView!.alpha = imageOffsetY / 188
        self.topImageView!.alpha = 1 - imageOffsetY / 358;
    }
    
    
    
    
    //MARK: 懒加载
     lazy var collectionView : UICollectionView = {
         
         
         let layout = UICollectionViewFlowLayout()
         //设置item的行间距.列间距
         layout.minimumLineSpacing = 7~
         layout.minimumInteritemSpacing = 7~
         
         //设置item的大小
         let invite = 40~
         let itemW = (kScreenWidth - invite)/2.0
         layout.itemSize = CGSize(width: itemW, height: 260~)
         layout.sectionInset = UIEdgeInsets(top: 0, left: 15~, bottom: 0, right: 15~)
         
         
         //设置区头 和区尾 大小
         layout.headerReferenceSize = CGSize(width: kScreenWidth, height: kisPhoneX() ? 798 : 798 - 24)
         layout.footerReferenceSize = CGSize(width: kScreenWidth, height: 10~)

         layout.scrollDirection = .vertical
         
         let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
        collectionView.backgroundColor = .clear;
        collectionView.dataSource = self
        collectionView.delegate = self;
        if #available(iOS 11.0, *) {
            collectionView.contentInsetAdjustmentBehavior = .never
        } else {
            // Fallback on earlier versions
        }

        
         return collectionView
     }()
      
}
